//
//  MCShippingAddressView.swift
//  StoreManage
//
//  Created by MC on 2017/12/27.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCShippingAddressViewDelegate : NSObjectProtocol {
    func MCShippingAddressViewSelected(province:String,city:String,area:String)
}

class MCShippingAddressView: UIView {

    weak var delegate : MCShippingAddressViewDelegate?
    
    private let componentCount = 3
    private var selectRowWithProvince = 0
    private var selectRowWithCity = 0
    private var selectRowWithTown = 0

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        backgroundColor = UIColor.MCGray_light
        self.frame = CGRect.init(x: 0, y: 0, width: 0, height: 216)
        self.addSubview(pickerView)
        pickerView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.top.equalTo(self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.showsSelectionIndicator = true
        return pickerView
    }()
    
    lazy var provinces: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var citys: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var areas: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}


extension MCShippingAddressView : UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return componentCount
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return provinces.count
        case 1:
            let subCity : NSArray = citys[selectRowWithProvince] as! NSArray
            return subCity.count
        case 2:
            let subCity : NSArray = areas[selectRowWithProvince] as! NSArray
            let subArea : NSArray = subCity[selectRowWithCity] as! NSArray
            return subArea.count
        default:
            return 0
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
     
        let pickerLabel = UILabel()
        pickerLabel.textAlignment = NSTextAlignment.center
        pickerLabel.adjustsFontSizeToFitWidth = true
        pickerLabel.backgroundColor = UIColor.clear
        pickerLabel.font = UIFont.boldSystemFont(ofSize: 14)
        pickerLabel.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
        
        return pickerLabel
    }
   
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        
        switch component {
        case 0:
            if (row >= provinces.count) {
                pickerView.selectRow(0, inComponent: 0, animated: false)
                return ""
            } else {
                return provinces[row%provinces.count] as? String
            }
        case 1:
            let pro = selectRowWithProvince
            let arr = citys[pro] as! NSArray
            if (row >= arr.count) {
                pickerView.selectRow(0, inComponent: 1, animated: false)
                return ""
            } else {
                return arr[row%arr.count] as? String
            }
            
        default:
            let pro = selectRowWithProvince
            var cit = selectRowWithCity
            let proV = areas[pro] as! NSArray
            if (cit >= proV.count) {
                cit = proV.count - 1
            }
            
            let arr = proV[cit] as! NSArray
            if (row >= arr.count) {
                pickerView.selectRow(0, inComponent: 2, animated: false)
                return ""
            } else {
                return arr[row%arr.count] as? String
            }
        }
    }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        
        if component == 0 {
            selectRowWithProvince = row
            selectRowWithCity = 0
            pickerView.reloadComponent(1)
            pickerView.reloadComponent(2)
        } else if component == 1 {
            selectRowWithCity = row
            pickerView.reloadComponent(2)
        } else {
            selectRowWithTown = row
        }
        
        let  pro = selectRowWithProvince
        let  cit = selectRowWithCity
        let  area = selectRowWithTown
        
        var provinceStr = ""
        var cityStr = ""
        var areaStr = ""

        if (provinces.count - 1) >= pro {
            provinceStr = provinces[pro] as! String
        } else {
            provinceStr = "暂无"
            pickerView.reloadComponent(0)
        }
        
        
        let areaArr = citys[pro] as! NSArray
        if (areaArr.count - 1) >= cit {
            cityStr = areaArr[cit] as! String
        } else {
            cityStr = "暂无"
            pickerView.reloadComponent(1)
        }
        
        
        let areas_oneArr = areas[pro] as! NSArray
        let areas_twoArr = areas_oneArr[cit] as! NSArray
        
        if (areas_twoArr.count - 1) >= area {
            areaStr = areas_twoArr[area] as! String
        } else {
            areaStr = "暂无"
            pickerView.reloadComponent(2)
        }
        
        self.delegate?.MCShippingAddressViewSelected(province: provinceStr, city: cityStr, area: areaStr)
    }
    
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if component == 0 {
            return MCScreenWidth/3 - 10
        } else if component == 2 {
            return MCScreenWidth / 3 + 5
        } else {
            return MCScreenWidth / 3 + 5
        }
    }
}
